package com.bootdo.kaoqin.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.kaoqin.domain.KaoqinCallGroup;
import com.bootdo.kaoqin.domain.KaoqinClass;
import com.bootdo.kaoqin.service.KaoqinCallGroupService;
import com.bootdo.kaoqin.service.KaoqinClassService;
import com.bootdo.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.kaoqin.domain.KaoqinUser;
import com.bootdo.kaoqin.service.KaoqinUserService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-09-26 19:49:32
 */
 
@Controller
@RequestMapping("/kaoqin/kaoqinUser")
public class KaoqinUserController extends BaseController{


	@Autowired
	private KaoqinUserService kaoqinUserService;
	@Autowired
	private KaoqinCallGroupService kaoqinCallGroupService;
	@Autowired
	private KaoqinClassService kaoqinClassService;

	@ModelAttribute
	public KaoqinUser get(@RequestParam(required=false) Long id) {
		if (id != null){
			return kaoqinUserService.get(id);
		}else{
			return new KaoqinUser();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/kaoqin/kaoqinUser','kaoqin:kaoqinUser:kaoqinUser',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','kaoqin:kaoqinUser:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("kaoqin:kaoqinUser:kaoqinUser")	
	String KaoqinUser(KaoqinUser kaoqinUser,HttpServletRequest request,HttpServletResponse response){
	    return "kaoqin/kaoqinUser/kaoqinUser";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("kaoqin:kaoqinUser:kaoqinUser")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<KaoqinUser> kaoqinUserList = kaoqinUserService.list(query);
		int total = kaoqinUserService.count(query);
		PageUtils pageUtils = new PageUtils(kaoqinUserList, total);
		return pageUtils;
	}


	@ResponseBody
	@PostMapping("/getListByUser")
	@RequiresPermissions("kaoqin:kaoqinUser:kaoqinUser")
	public R getListByUser(){
		List<KaoqinUser> kaoqinUserList = kaoqinUserService.list(MapBuild
				.getInstand().setKV("userId",ShiroUtils.getUserId()));
		return R.ok().put("l",kaoqinUserList);
	}

	
	@GetMapping("/add")
	@RequiresPermissions("kaoqin:kaoqinUser:edit")
	String add(KaoqinUser kaoqinUser,HttpServletRequest request,HttpServletResponse response){
	    return "kaoqin/kaoqinUser/kaoqinUseredit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(KaoqinUser kaoqinUser) {
		return R.ok().put("i",kaoqinUser);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("kaoqin:kaoqinUser:edit")
	public R save( KaoqinUser kaoqinUser){
		if(null != kaoqinUser.getGroupId()){
			 KaoqinCallGroup kaoqinCallGroup =  kaoqinCallGroupService.get(kaoqinUser.getGroupId());
			 if(null != kaoqinCallGroup){
			 	kaoqinUser.setGroupId(kaoqinCallGroup.getId());
			 	kaoqinUser.setGroupName(kaoqinCallGroup.getName());
			 }
		}
		if(null == kaoqinUser.getUserId()){
			UserDO userDO =  ShiroUtils.getUser();
			kaoqinUser.setUserId(userDO.getUserId());
			kaoqinUser.setUserName(userDO.getUsername());
		}
		if(null == kaoqinUser.getClassId()){
			return R.error("部门ID不能为空");
		}else{
		 	KaoqinClass kaoqinClass =  kaoqinClassService.get(kaoqinUser.getClassId().intValue());
		 	if(null != kaoqinClass){
		 		 kaoqinUser.setClassName(kaoqinClass.getName());
			}
		}



		if(kaoqinUserService.save(kaoqinUser)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("kaoqin:kaoqinUser:edit")
	public R remove( Long id){
		if(kaoqinUserService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("kaoqin:kaoqinUser:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		kaoqinUserService.batchRemove(ids);
		return R.ok();
	}
	
}